﻿using PdfChartHelper.Models;
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PdfChartHelper.Writer
{
    public class PieWriter
    {
        /// <summary>
        /// 从模型化饼图
        /// </summary>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static PdfDocument DrawPieChartInModel(int X, int Y, int radius, List<PieModel> list)
        {

            #region pdf设置
            PdfDocument doc = new PdfDocument();

            PdfPage page = new PdfPage(doc);

            XGraphics graphics = XGraphics.FromPdfPage(page);

            graphics.TranslateTransform(100, 100);

            XPen bian = new XPen(XColors.Aqua, 1.0);

            #endregion
            //先获取总量
            double sum = list.Sum(item => item.Num);

            //再获取面一个元素所需要占用的角度


            list.ForEach(item => {

                item.single = (item.Num / sum) * 360;


            });

            //-------------------------------画饼图
            double tempCount = 0;
            list.ForEach(item => {

                graphics.DrawPie(new XPen(item.color, 1), item.b_color, X, Y, radius, radius, tempCount, item.single);

                tempCount += item.single;

            });

            //------------------------------列出成分

            XStringFormat format = new XStringFormat();

            int temp = 0;
            list.ForEach(item => {
                graphics.DrawRectangle(item.b_color, X + temp, Y + radius + 15, radius / 5, radius / 7);
                graphics.DrawString(item.DESC, new XFont("Arial", 16, XFontStyle.Regular),
                    XBrushes.Black, X + temp + radius / 5, Y + radius + 15 + radius / 8);
                temp += 80;
            });

            doc.AddPage(page);
            return doc;

        }

    }
}
